﻿@model IEnumerable<Swift.Core.JobConfig>

@{
    ViewBag.Title = "作业";
}

<h2>集群 @ViewBag.ClusterName 作业</h2>

@section header{
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/css/fileinput.min.css" media="all" rel="stylesheet" type="text/css" />
}

@section scripts{
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/js/plugins/piexif.min.js" type="text/javascript"></script>
    <!-- sortable.min.js is only needed if you wish to sort / rearrange files in initial preview.
        This must be loaded before fileinput.min.js -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/js/plugins/sortable.min.js" type="text/javascript"></script>
    <!-- purify.min.js is only needed if you wish to purify HTML content in your preview for
        HTML files. This must be loaded before fileinput.min.js -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/js/plugins/purify.min.js" type="text/javascript"></script>
    <!-- the main fileinput plugin file -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/js/fileinput.min.js"></script>
    <!-- optionally if you need a theme like font awesome theme you can include it as mentioned below -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/themes/fa/theme.js"></script>
    <!-- optionally if you need translation for your language then include  locale file as mentioned below -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/js/locales/zh.js"></script>
    <script type="text/javascript">
    $("#jobPkgFile").fileinput({
            uploadUrl:"/Job/UploadPackage",//上传的地址
            uploadAsync: true,
            language : "zh",//设置语言
            showCaption: true,//是否显示标题
            showPreview:false,
            showUpload: false, //是否显示上传按钮
            dropZoneEnabled:false, //是否允许拖拽上传
            browseClass: "btn btn-primary", //按钮样式
            allowedFileExtensions: ["zip"], //接收的文件后缀
            maxFileCount: 1,//最大上传文件数限制
            uploadAsync: true,
            uploadExtraData: function() {
                return {"clustername":"@ViewBag.ClusterName"};
            }
    });

    $("#jobPkgFile").on("fileuploaded", function(event, data, previewId, index) {
        alert("发布成功!");
        window.location.reload();
    });

    $('#jobPkgFile').on('fileuploaderror', function(event, data, msg) {
        alert("发布失败："+msg);
        window.location.reload();
    });

   $("#btnUploadPkgFile").click(function(){
        $('#jobPkgFile').fileinput('upload');
   });

  $('#commonModal').on('hide.bs.modal', function () {
        window.location.reload();
  });

   function RunJob(clusterName,jobName){
        $.ajax({
            url: "@Url.Action("Run","Job")",
            type:"GET",
            dataType:"json",
            data:{"clusterName":clusterName,"jobName":jobName},
            success: function(data,textStatus){
                if(data.errCode==0){
                        $("#commonModal .modal-title").text("提交成功");
                        $("#commonModal .modal-body").text("任务已经创建，将很快启动运行。");
                        $('#commonModal').modal({ keyboard: false });
                }else{
                        $("#commonModal .modal-title").text("提交失败");
                        $("#commonModal .modal-body").text(data.errMessage);
                        $('#commonModal').modal({ keyboard: false });
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                alert("提交失败");
            }
        });
    }

    function ShowJobConfig(clusterName,jobName){
        $.ajax({
            url: "@Url.Action("GetJobConfig","Job")",
            type:"GET",
            dataType:"json",
            data:{"clusterName":clusterName,"jobName":jobName},
            success: function(result,textStatus){
                if(result.errCode==0){
                        $("#commonModal .modal-title").text(result.data.name);
                        var configHtml="<table class=\"table table-striped table-hover\" style=\"word-break:break-all; word-wrap:break-all;\">";
                        configHtml+="<tr>";
                        configHtml+="<td style='width:200px'>版本</td>";
                        configHtml+="<td style='width:368px'>"+result.data.version+"</td>";
                        configHtml+="</tr>";
                        configHtml+="<tr>";
                        configHtml+="<td>可执行文件类型</td>";
                        configHtml+="<td>"+result.data.exeType+"</td>";
                        configHtml+="</tr>";
                        configHtml+="<tr>";
                        configHtml+="<td>可执行文件名称</td>";
                        configHtml+="<td>"+result.data.fileName+"</td>";
                        configHtml+="</tr>";
                        configHtml+="<tr>";
                        configHtml+="<td>作业类</td>";
                        configHtml+="<td>"+result.data.jobClassName+"</td>";
                        configHtml+="</tr>";
                        configHtml+="<tr>";
                        configHtml+="<td>执行计划</td>";
                        configHtml+="<td>"+result.data.runTimePlan+"</td>";
                        configHtml+="</tr>";
                        configHtml+="<tr>";
                        configHtml+="<td>成员不可用确认阈值</td>";
                        configHtml+="<td>"+result.data.memberUnavailableThreshold+"分钟</td>";
                        configHtml+="</tr>";
                        configHtml+="<tr>";
                        configHtml+="<td>作业分割执行超时时间</td>";
                        configHtml+="<td>"+result.data.jobSplitTimeout+"分钟</td>";
                        configHtml+="</tr>";
                        configHtml+="<tr>";
                        configHtml+="<td>单个任务执行超时时间</td>";
                        configHtml+="<td>"+result.data.taskExecuteTimeout+"分钟</td>";
                        configHtml+="</tr>";
                        configHtml+="<tr>";
                        configHtml+="<td>结果合并执行超时时间</td>";
                        configHtml+="<td>"+result.data.taskResultCollectTimeout+"分钟</td>";
                        configHtml+="</tr>";
                        configHtml+="</table>";
                        $("#commonModal .modal-body").html(configHtml);
                        $('#commonModal').modal({ keyboard: false });
                }else{
                        $("#commonModal .modal-title").text(jobName);
                        $("#commonModal .modal-body").text(data.errMessage);
                        $('#commonModal').modal({ keyboard: false });
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                alert("获取作业配置信息失败！");
            }
        });
    }
    </script>
}

<p>
    <a class="btn btn-info" href="@Url.Action("Index","Home")">返回</a>
    <a class="btn btn-info" href="javascript:location.reload()">刷新</a>
</p>

<div id="job-publish">
    <h3>发布作业</h3>
    <p><input id="jobPkgFile" name="jobPkgFile" class="flie" type="file"></p>
    <p class="text-right"><button id="btnUploadPkgFile" type="button" class="btn btn-primary">上传并发布</button></p>
    @*<div class="row">
            <div class="col-md-9"></div>
            <div class="col-md-3 vertical-center"></div>
        </div>*@
</div>

<div id="jobs-list">
    <table class="table table-striped table-hover">
        <tr>
            <th>
                作业名称
            </th>
            <th>
                当前版本
            </th>
            <th>
                执行计划
            </th>
            <th>
                最近执行时间
            </th>
            <th>
                操作
            </th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Version)
                </td>
                <td>
                    @string.Join(",", item.RunTimePlan)
                </td>
                <td>
                    @(item.LastRecordCreateTime.HasValue ? item.LastRecordCreateTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : string.Empty)
                </td>
                <td>
                    <a href="javascript:ShowJobConfig('@ViewBag.ClusterName','@item.Name')">配置信息</a>
                    @Html.ActionLink("运行记录", "JobRecords", new { job = item.Name, cluster = ViewBag.ClusterName })
                    <a href="javascript:RunJob('@ViewBag.ClusterName','@item.Name')">立即运行</a>
                </td>
            </tr>
        }
    </table>
</div>

<div class="modal fade" id="commonModal" tabindex="-1" role="dialog" aria-labelledby="commonModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="commonModalLabel"></h4>
            </div>
            <div class="modal-body">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>